package ferp.android.managers;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import ferp.android.ads.MediationAdManager;
import ferp.android.ads.interstitial.InterstitialAdManager;
import ferp.core.log.Log;
import ferp.core.player.Signature;

/**
 * User: olegs
 * Date: 14/07/2014 15:59
 */
public class BonusManager
{
  public static void activateForPoll(Activity activity, MediationAdManager adManager, int bonus)
  {
    SharedPreferences sp = activity.getApplicationContext().getSharedPreferences(AD_MANAGER, Context.MODE_PRIVATE);
    long now = System.currentTimeMillis();
    long expires = Math.max(sp.getLong(BONUS_EXPIRATION_DATE, now), now) + bonus * MS_IN_DAYS;

    SharedPreferences.Editor e = sp.edit();

    // save the expiration date
    e.putLong(BONUS_EXPIRATION_DATE, expires);
    // produce and save authenticity signature
    e.putString(BONUS_VALUE, Signature.generate(String.valueOf(expires)));

    e.commit();

    adManager.stop();
    InterstitialAdManager.stop();

    // notify user
    //Resources r = context.getResources();
    //GUI.toast(context, r.getString(R.string.toast_bonus_one_time, r.getString(toast), String.valueOf((expires - now) / MS_IN_DAY)), Toast.LENGTH_LONG);
  }

  public static void activateOneTimeFacebookBonus(Activity activity) {/*activateOneTimeBonus(activity, FACEBOOK_BONUS_ACTIVATED, R.string.toast_bonus_one_time_facebook_like);*/}

  public static void activateOneTimeGooglePlusBonus(Activity activity) {/*activateOneTimeBonus(activity, GOOGLE_PLUS_BONUS_ACTIVATED, R.string.toast_bonus_one_time_google_plus_one);*/}

  public static boolean isActive(Activity activity)
  {
    SharedPreferences sp = activity.getApplicationContext().getSharedPreferences(AD_MANAGER, Context.MODE_PRIVATE);
    long now = System.currentTimeMillis(), expiration = sp.getLong(BONUS_EXPIRATION_DATE, now);

  /*
        // debug !!!
        SharedPreferences.Editor e = sp.edit();
        e.putLong(BONUS_EXPIRATION_DATE, now - 1);
        e.commit();
  */

    boolean active = now < expiration && Signature.verify(sp.getString(BONUS_VALUE, ""), String.valueOf(expiration));

    if (active)
      Log.debug(Log.TAG, "bonus is active for another " + ((expiration - now) / 1000 / 60 / 60) + " hours");

    return active;
  }

  // preference keys
  private static final String AD_MANAGER                  = "am";
  //private static final String FACEBOOK_BONUS_ACTIVATED    = "fb";
  //private static final String GOOGLE_PLUS_BONUS_ACTIVATED = "gp";
  private static final String POLL_BONUS_ACTIVATED        = "po";
  private static final String BONUS_EXPIRATION_DATE       = "bed";
  private static final String BONUS_VALUE                 = "v";

  // number of msec in day
  private static final long MS_IN_DAYS = 1000 * 60 * 60 * 24;

/*
  private static boolean activateOneTimeBonus(Activity activity, String name, int bonus, int toast)
  {
    SharedPreferences sp = activity.getSharedPreferences(AD_MANAGER, Context.MODE_PRIVATE);
    boolean activated = sp.getBoolean(name, false);

    // activate bonus only once
    if (activated)
      return false;

    long now = System.currentTimeMillis();
    long expires = Math.max(sp.getLong(BONUS_EXPIRATION_DATE, now), now) + bonus * MS_IN_HOUR;

    SharedPreferences.Editor e = sp.edit();

    // one time bonus activated
    e.putBoolean(name, true);
    // save the expiration date
    e.putLong(BONUS_EXPIRATION_DATE, expires);
    // signature
    e.putString(BONUS_VALUE, calculate(expires, true, false));

    e.commit();

    final View mask = adsMask(activity);
    final AdView av = ad(activity);

    if (mask != null)
      mask.setVisibility(View.GONE);
    if (av != null)
      av.setVisibility(View.GONE);

    // notify user
    //Resources r = context.getResources();
    //GUI.toast(context, r.getString(R.string.toast_bonus_one_time, r.getString(toast), String.valueOf((expires - now) / MS_IN_DAY)), Toast.LENGTH_LONG);

    return true;
  }
*/
}
